<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="../../ext/resources/css/ext-all.css" />

    <script type="text/javascript" src="../../../openlayers/lib/Firebug/firebug.js"></script>
    <script type="text/javascript" src="../../../openlayers/lib/OpenLayers.js"></script>

    <script type="text/javascript" src="../../../ext/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="../../../ext/ext-all-debug.js"></script>

    <script type="text/javascript">
      // Because of a bug in Firefox 2 we need to specify the MapFish base path.
      // See https://bugzilla.mozilla.org/show_bug.cgi?id=351282
      var gMfLocation = "../../../mapfish/";
    </script>
    <script type="text/javascript" src="../../../mapfish/MapFish.js"></script>

    <script type="text/javascript"><!--

    function test_Offline(t) {
        t.plan(51);

        var MO = mapfish.Offline;
        var TIMEOUT = 200;

        t.wait_result(5);

        var lastSyncTaskLabel;
        var activeSyncTasks = 0;
        function syncTaskEventHandler(syncTaskEvent) {
            if (syncTaskEvent.type == "syncTaskAdded") {
                activeSyncTasks++;
            } else if (syncTaskEvent.type == "syncTaskDone") {
                activeSyncTasks--;
            } else {
                throw new Error("Unknown syncTask type " + syncTaskEvent.type);
            }
            lastSyncTaskLabel = syncTaskEvent.syncTask.label;
        }

        var seenDownloadDone = false;
        var seenUploadDone = false;
        function syncEventHandler(type) {
            if (type == "downloadDone") {
                seenDownloadDone = true;
            }
            if (type == "uploadDone") {
                seenUploadDone = true;
            }
        }

        function next_0() {
            t.eq(MO.isOnline, true, "online at startup");
            MO.goOffline();
            var seenOfflineEvent = false;
            MO.events.register("network", null, function(status) {
                MO.events.unregister("network", null, arguments.callee);
                if (status == "offline")
                    seenOfflineEvent = true;
            });
            setTimeout(function() {
                t.eq(seenOfflineEvent, true, "offline event fired");
                next_1();
            }, TIMEOUT);
        }

        function next_1() {
            t.eq(MO.isOnline, false, "went offline");

            // Shouldn't fire network event if already offline
            var seenEvent = false;
            MO.events.register("network", null, function(status) {
                MO.events.unregister("network", null, arguments.callee);
                seenEvent = true;
            });
            setTimeout(function() {
                t.eq(seenEvent, false, "No event fired");

                MO.goOnline();
                setTimeout(function() {
                    next_2();
                }, TIMEOUT);
            }, TIMEOUT);
        }

        function next_2() {
            t.eq(MO.isOnline, true, "went online");

            var syncTaskId1, syncTaskId2, syncTaskId3;
            MO.events.register("synctask", null, syncTaskEventHandler);

            MO.events.register("sync", null, function(type) {
                if (type == "download") {
                    MO.events.unregister("sync", null, arguments.callee);
                    t.eq(activeSyncTasks, 0, "no syncTask yet")

                    syncTaskId1 = MO.addSyncTask("Sample syncTask 1");
                    t.eq(activeSyncTasks, 1, "1 active syncTask")
                    t.eq(lastSyncTaskLabel, "Sample syncTask 1", "correct last task label");

                    // for syncTask 2, addSyncTask and syncTaskDone are called
                    // in the same execution flow, in that case check that downloadDone is
                    // not fired after syncTaskDone
                    syncTaskId2 = MO.addSyncTask("Sample syncTask 2");
                    t.eq(activeSyncTasks, 2, "2 active syncTasks")
                    t.eq(lastSyncTaskLabel, "Sample syncTask 2", "correct last task label");
                    MO.syncTaskDone(syncTaskId2);
                    t.eq(seenDownloadDone, false, "downloadDone not yet fired");
                    t.eq(activeSyncTasks, 1, "1 active syncTasks");

                    syncTaskId3 = MO.addSyncTask("Sample syncTask 3");
                    t.eq(activeSyncTasks, 2, "2 active syncTasks")
                    t.eq(lastSyncTaskLabel, "Sample syncTask 3", "correct last task label");
                }
            });

            MO.events.register("sync", null, syncEventHandler);

            t.eq(MO.syncState, null, "not syncing");
            MO.goOffline();

            setTimeout(function() {
                t.eq(MO.isOnline, true, "Not yet offline, syncTasks are pending");
                t.eq(MO.syncState, "download", "sync download");

                t.eq(activeSyncTasks, 2, "2 active tasks")
                MO.syncTaskDone(syncTaskId1);
                t.eq(activeSyncTasks, 1, "1 active tasks")
                t.eq(lastSyncTaskLabel, "Sample syncTask 1", "correct last task label");
                t.eq(seenDownloadDone, false, "downloadDone not yet fired");

                t.eq(MO.isOnline, true, "Not yet offline, syncTasks are pending");
                MO.syncTaskDone(syncTaskId3);
                t.eq(activeSyncTasks, 0, "0 active tasks")
                t.eq(lastSyncTaskLabel, "Sample syncTask 3", "correct last task label");
                t.eq(seenDownloadDone, false, "downloadDone not yet fired");

                MO.events.unregister("synctask", null, syncTaskEventHandler);
                setTimeout(function() {
                    MO.events.unregister("sync", null, syncEventHandler);
                    next_3();
                }, TIMEOUT);
            }, TIMEOUT);
        }

        function next_3() {
            t.eq(seenDownloadDone, true, "downloadDone fired");
            seenDownloadDone = false;
            t.eq(MO.isOnline, false, "went offline, syncTasks are done");
            t.eq(MO.syncState, null, "finished sync");

            var syncTaskId1, syncTaskId2, syncTaskId3;
            MO.events.register("synctask", null, syncTaskEventHandler);

            MO.events.register("sync", null, function(type) {
                if (type == "upload") {
                    MO.events.unregister("sync", null, arguments.callee);
                    t.eq(activeSyncTasks, 0, "no syncTask yet")

                    syncTaskId1 = MO.addSyncTask("Sample syncTask 1");
                    t.eq(activeSyncTasks, 1, "1 active syncTask")
                    t.eq(lastSyncTaskLabel, "Sample syncTask 1", "correct last task label");

                    // for syncTask 2, addSyncTask and syncTaskDone are called
                    // in the same execution flow, in that case check that downloadDone is
                    // not fired after syncTaskDone
                    syncTaskId2 = MO.addSyncTask("Sample syncTask 2");
                    t.eq(activeSyncTasks, 2, "2 active syncTasks")
                    t.eq(lastSyncTaskLabel, "Sample syncTask 2", "correct last task label");
                    MO.syncTaskDone(syncTaskId2);
                    t.eq(seenUploadDone, false, "uploadDone not yet fired");
                    t.eq(activeSyncTasks, 1, "1 active syncTasks");

                    syncTaskId3 = MO.addSyncTask("Sample syncTask 3");
                    t.eq(activeSyncTasks, 2, "2 active syncTasks")
                    t.eq(lastSyncTaskLabel, "Sample syncTask 3", "correct last task label");
                }
            });

            MO.events.register("sync", null, syncEventHandler);

            t.eq(MO.syncState, null, "not syncing");
            MO.goOnline();

            setTimeout(function() {
                t.eq(MO.isOnline, true, "went online, synchronizing upload");
                t.eq(MO.syncState, "upload", "sync upload");

                t.eq(activeSyncTasks, 2, "2 active tasks")
                MO.syncTaskDone(syncTaskId1);
                t.eq(activeSyncTasks, 1, "1 active tasks")
                t.eq(lastSyncTaskLabel, "Sample syncTask 1", "correct last task label");
                t.eq(seenUploadDone, false, "uploadDone not yet fired");

                t.eq(MO.isOnline, true, "Not yet offline, syncTasks are pending");
                MO.syncTaskDone(syncTaskId3);
                t.eq(activeSyncTasks, 0, "0 active tasks")
                t.eq(lastSyncTaskLabel, "Sample syncTask 3", "correct last task label");
                t.eq(seenUploadDone, false, "uploadDone not yet fired");

                setTimeout(function() {
                    MO.events.unregister("sync", null, syncEventHandler);
                    next_4();
                }, TIMEOUT);
            }, TIMEOUT);
        }

        function next_4() {
            t.eq(seenUploadDone, true, "uploadDone fired");
            seenUploadDone = false;
            t.eq(MO.isOnline, true, "went online, syncTasks are done");
            t.eq(MO.syncState, null, "finished sync");
        }

        next_0();

    }
--></script>

  </head>
  <body>
  </body>
</html>
